#include "header.h"
const char *building_version=__VERSION_ID__;
void printHelp()
{
    cout << "Usage: getSim_latent -vh [-c configFile] DOC_NUM latent_feature_file dst_similarity_file [-s start] [-e end]" << endl;
}

void print_building_info(void)
{ 
    printf("Building version:\t%s\nBuilding time:\t\t%s %s\n", building_version, __DATE__, __TIME__);
}

ConfigFile g_config;
RelatedQuestions g_relateQuestions;
DBBase g_db;

int main (int argc, char* argv[])
{
    //cout << argc << endl;
    const char *optstring = "hvc:s:e:";
    char configFile[MAX_FILE_NAME_LEN];
    int optch = 0;
    int doc_num = 0, start = 0, end = 0;
    const char *featureFile, *simFile, *inLinkFile, *relateFile;
    string simFun;
    while ((optch = getopt(argc, argv, optstring)) != -1) {
        switch (optch) {
            case 'c':
                strcpy(configFile, optarg);
                break;
            case 's':
                start = atoi(optarg);
                break;
            case 'e':
                end = atoi(optarg);
                break;
            case 'h':
                printHelp();
                return 0;
            case 'v':
                print_building_info();
                return 0;
        }
    }
    if (strlen(configFile) < 1) {
        if (argc < 6) {
            printHelp();
            exit(1);
        }
        else {
            doc_num = atoi(argv[1]);
            featureFile = argv[2];
            simFile = argv[3];
            start = atoi(argv[4]);
            end = atoi(argv[5]);
            inLinkFile = "";
            relateFile = "";
        }
    }
    else {
        g_config.load(configFile);
        doc_num = atoi(g_config.Value("section_1", "doc_num").c_str());
        featureFile = g_config.Value("section_1", "feature_file").c_str();
        simFile = g_config.Value("section_1", "sim_file").c_str();
        if (start == 0) start = atoi(g_config.Value("section_1", "start").c_str());
        if (end ==0) end = atoi(g_config.Value("section_1", "end").c_str());
        simFun = g_config.Value("section_1", "sim_fun").c_str();
        if ("IIL" == simFun) {
            inLinkFile = g_config.Value("section_1", "inlink_file").c_str();
            relateFile = g_config.Value("section_1", "relateQuestion_file").c_str();
        } else {
            inLinkFile = "";
            relateFile = "";
        }
    }
    vector< map<int, float> > features(doc_num+1);
    const int neighb_num = NEIGHB_NUM;
    if ("IIL" == simFun) {
        g_relateQuestions.load(relateFile, inLinkFile, doc_num, neighb_num);
    }
    if (1 == atoi(g_config.Value("database","isSaveDb").c_str())){
        //host,db,user,pass
        int conRet = g_db.openConnection(g_config.Value("database", "server").c_str(),
                            g_config.Value("database", "dbname").c_str(),
                            g_config.Value("database", "user").c_str(),
                            g_config.Value("database", "password").c_str());
        if (200 != conRet) {
            cout << dberror(conRet);
            exit(1);
        }
    }
    //首先初始化变量，将每个文档的向量空间模型描述放入vector数组中
    int itemNum = loadTfidfFeatures(features, featureFile);
    if (itemNum != doc_num) {
        cout << "输入的item数目和读取到的item数目不符，请检查！" <<endl;
        exit(1);
    }
    //calculate  the sim and sort in a heap
    calTFIDFSimAndSave(features, doc_num, simFile, start, end, simFun, g_relateQuestions);
    return 0;
}
